package com.sunwayworld.basemodule.rm.train.examtmpl.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.sunwayworld.basemodule.rm.train.exam.bean.LimsCoreExamBean;
import com.sunwayworld.basemodule.rm.train.exam.service.LimsCoreExamService;
import com.sunwayworld.basemodule.rm.train.examtmpl.bean.LimsCoreExamTmplBean;
import com.sunwayworld.basemodule.rm.train.examtmpl.dao.LimsCoreExamTmplDao;
import com.sunwayworld.basemodule.rm.train.examtmpl.service.LimsCoreExamTmplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.audit.constant.AuditConstant;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.restful.data.RestValidationResultBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;

@GikamBean
@Repository
public class LimsCoreExamTmplServiceImpl implements LimsCoreExamTmplService {
    @Autowired
    private LimsCoreExamTmplDao limsCoreExamTmplDao;
    
    @Autowired
    @Lazy
    private LimsCoreExamService limsCoreExamService;
    
    @Override
    @SuppressWarnings("unchecked")
    public LimsCoreExamTmplDao getDao() {
        return limsCoreExamTmplDao;
    }
    
    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    @Audit(AuditConstant.INSERT)
    public Long insert(RestJsonWrapperBean wrapper) {
        LimsCoreExamTmplBean examTmpl = wrapper.parseUnique(LimsCoreExamTmplBean.class);
        examTmpl.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(examTmpl);
        return examTmpl.getId();
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    @Audit(AuditConstant.INSERT)
    public List<Long> clone(RestJsonWrapperBean wrapper) {
        List<LimsCoreExamTmplBean> examTmplList = wrapper.parse(LimsCoreExamTmplBean.class);
        if (examTmplList.isEmpty()) {
            return null;
        }
        
        List<LimsCoreExamTmplBean> examTmplList4Insert = new ArrayList<>();
        List<Long> idList = new ArrayList<>();
        
        examTmplList.forEach(e -> {
            LimsCoreExamTmplBean examTmpl4Insert = new LimsCoreExamTmplBean();
            Long id = ApplicationContextHelper.getNextIdentity();
            
            examTmpl4Insert.setId(id);
            examTmpl4Insert.setTmplName(e.getTmplName());
            examTmpl4Insert.setSingleChoiceQty(e.getSingleChoiceQty());
            examTmpl4Insert.setSingleChoiceScore(e.getSingleChoiceScore());
            examTmpl4Insert.setMultipleChoiceQty(e.getMultipleChoiceQty());
            examTmpl4Insert.setMultipleChoiceScore(e.getMultipleChoiceScore());
            examTmpl4Insert.setCompletionQty(e.getCompletionQty());
            examTmpl4Insert.setCompletionScore(e.getCompletionScore());
            examTmpl4Insert.setJudgementQty(e.getJudgementQty());
            examTmpl4Insert.setJudgementScore(e.getJudgementScore());
            examTmpl4Insert.setShortAnswerQty(e.getShortAnswerQty());
            examTmpl4Insert.setShortAnswerScore(e.getShortAnswerScore());
            examTmpl4Insert.setTotalScore(e.getTotalScore());
            
            idList.add(id);
            examTmplList4Insert.add(examTmpl4Insert);
        });
        
        getDao().insert(examTmplList4Insert);
        return idList;
    }
    
    @Override
    public RestValidationResultBean deleteValidate(RestJsonWrapperBean wrapper) {
        List<LimsCoreExamTmplBean> examTmplList = wrapper.parse(LimsCoreExamTmplBean.class);
        
        boolean validateFlag = true;
        StringBuilder messageBuilder = new StringBuilder();
        
        List<Long> examTmplIdList = examTmplList.stream().map(f -> f.getId()).collect(Collectors.toList());
        List<LimsCoreExamBean> examList = limsCoreExamService.selectListByFilter(SearchFilter.instance().match("EXAMTMPLID", examTmplIdList).filter(MatchPattern.OR));

        for (LimsCoreExamTmplBean examTmpl : examTmplList) {
            if (examList.stream().anyMatch(f -> f.getExamTmplId().equals(examTmpl.getId()))) {
                validateFlag = false;
                messageBuilder.append(examTmpl.getTmplName() + ":" +I18nHelper.getMessage("CORE.MODULE.TRAIN.EXAM_TMPL.TIP.QUOTED_BY_EXAM_CANNOT_DELETE") + "<br />");
            }
        }

        return new RestValidationResultBean(validateFlag, messageBuilder.toString());
    }
}